home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWFontLi.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  8.2 KB  |  288 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFontLi.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWFONTLI_H
  13. #include "FWFontLi.h"
  14. #endif
  15.  
  16. // ----- Foundation Includes -----
  17.  
  18. #ifndef FWMEMMGR_H
  19. #include "FWMemMgr.h"
  20. #endif
  21.  
  22. #ifndef FWSTRING_H
  23. #include "FWString.h"
  24. #endif
  25.  
  26. #ifndef _ITEXT_
  27. #include "IText.h"
  28. #endif
  29.  
  30. // ----- Platform Includes -----
  31.  
  32. #ifdef FW_BUILD_WIN
  33. #include <windows.h>
  34. #endif
  35.  
  36. #if defined(FW_BUILD_MAC) && !defined(__MENUS__)
  37. #include <Menus.h>
  38. #endif
  39.  
  40. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  41. #include <Script.h>
  42. #endif
  43.  
  44. //========================================================================================
  45. // Runtime infos
  46. //========================================================================================
  47.  
  48. #ifdef FW_BUILD_MAC
  49. #pragma segment FWGraphx_FontList
  50. #endif
  51.  
  52. //========================================================================================
  53. //    Template Instantiations
  54. //========================================================================================
  55.  
  56. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollectionIterator, FW_CString)
  57. FW_DEFINE_AUTO_TEMPLATE(FW_TOrderedCollection, FW_CString)
  58.  
  59. #ifdef FW_USE_TEMPLATE_PRAGMAS
  60.  
  61. #pragma template_access public
  62. #pragma template FW_TOrderedCollection<FW_CString>
  63. #pragma template FW_TOrderedCollectionIterator<FW_CString>
  64.  
  65. #endif
  66.  
  67. //========================================================================================
  68. // CLASS FW_CFontList
  69. //========================================================================================
  70.  
  71. FW_DEFINE_AUTO(FW_CFontList)
  72.  
  73. #ifdef FW_BUILD_WIN
  74. //----------------------------------------------------------------------------------------
  75. // PrivWinFillFontList
  76. //
  77. // Windows callback method to insert font names into the supplied list.
  78. //----------------------------------------------------------------------------------------
  79.  
  80. static int CALLBACK PrivWinFillFontList(const LOGFONT*         lplf,
  81.                                         const TEXTMETRIC*    /* lptm */,
  82.                                         DWORD                 /* dwFontType */,
  83.                                         LPARAM                 lParam)
  84. {
  85.     FW_TOrderedCollection<FW_CString>* list = (FW_TOrderedCollection<FW_CString>*) lParam;
  86.     FW_CString* fontName = new FW_CString((FW_Char*) lplf->lfFaceName);
  87.     list->AddLast(fontName);
  88.     return TRUE;        // Keep going
  89. }
  90. #endif
  91.  
  92. //----------------------------------------------------------------------------------------
  93. //    FW_CFontList::FW_CFontList
  94. //----------------------------------------------------------------------------------------
  95.  
  96. FW_CFontList::FW_CFontList()
  97. {
  98. #ifdef FW_BUILD_MAC
  99.     const short dummyMenuID = 7777;
  100.     MenuHandle fontListHolder = ::NewMenu(dummyMenuID, "\pODF");
  101.     
  102.     if (fontListHolder == NULL)
  103.         FW_Failure(FW_xMemoryExhausted);
  104.  
  105.     short count = 0;
  106.  
  107.     FW_TRY
  108.     {
  109.         ::AppendResMenu(fontListHolder, 'FONT');
  110.         count = ::CountMItems(fontListHolder);
  111.         Str255 itemName;
  112.  
  113.         // get System script code and language code
  114.         long systemScriptCode = ::GetScriptManagerVariable(smSysScript);
  115.         long systemLanguageCode = ::GetScriptVariable(systemScriptCode, smScriptLang);
  116.         short itemField;
  117.  
  118.         for (short k = 1; k <= count; k++)
  119.         {
  120.             ::GetMenuItemText(fontListHolder, k, itemName);
  121.             ::GetItemCmd(fontListHolder, k, &itemField);    // Check cmd key equivalent code
  122.  
  123.             ODScriptCode scriptCode = (ODScriptCode) systemScriptCode;
  124.             ODLangCode languageCode = (ODLangCode) systemLanguageCode;
  125.  
  126.             if (itemField == 0x1C)
  127.                     // key equiv value of 0x1C indicates that the icon field contains a script code
  128.             {
  129.                 ::GetItemIcon(fontListHolder, k, &itemField);    // get script code from icon field
  130.                 scriptCode = (ODScriptCode) itemField;
  131.                 if (itemField == 0x00FF)     /* kludge */
  132.                     scriptCode = (ODScriptCode) systemScriptCode;
  133.                 // Get the language code from the script code
  134.                 languageCode = (ODLangCode) ::GetScriptVariable(scriptCode, smScriptLang);
  135.             }
  136.  
  137.             FW_CString* fontName = FW_NEW(FW_CString, ());
  138.             ODIText* text = CreateIText(scriptCode, languageCode, itemName);
  139.             fontName->ReplaceAll(text);
  140.             DisposeIText(text);
  141.             fCollection.AddLast(fontName);
  142.         }
  143.         ::DisposeMenu(fontListHolder);
  144.     }
  145.     FW_CATCH_BEGIN
  146.     FW_CATCH_EVERYTHING()
  147.     {
  148.         ::DisposeMenu(fontListHolder);
  149.         FW_THROW_SAME();
  150.     }
  151.     FW_CATCH_END
  152. #endif
  153.  
  154. #ifdef FW_BUILD_WIN
  155.     HDC dc = ::GetDC(NULL);
  156.     if(dc != 0)
  157.     {
  158.         FW_TRY
  159.         {
  160.             ::EnumFontFamilies(dc, NULL, ::PrivWinFillFontList, (LPARAM)&fCollection);
  161.         }
  162.         FW_CATCH_BEGIN
  163.         FW_CATCH_EVERYTHING()
  164.         {
  165.             ::ReleaseDC(NULL, dc);
  166.             FW_THROW_SAME();
  167.         }
  168.         FW_CATCH_END
  169.         ::ReleaseDC(NULL, dc);
  170.     }
  171. #endif
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. //    FW_CFontList::~FW_CFontList
  176. //----------------------------------------------------------------------------------------
  177.  
  178. FW_CFontList::~FW_CFontList()
  179. {
  180.     FW_TOrderedCollectionIterator<FW_CString> ite(&fCollection);
  181.     for (FW_CString* fontName = ite.First(); ite.IsNotComplete(); fontName = ite.Next())
  182.     {
  183.         delete fontName;
  184.     }
  185. }
  186.  
  187. //========================================================================================
  188. // CLASS FW_CFontIterator
  189. //========================================================================================
  190.  
  191. FW_DEFINE_AUTO(FW_CFontIterator)
  192.  
  193. //----------------------------------------------------------------------------------------
  194. // FW_CFontIterator::FW_CFontIterator
  195. //----------------------------------------------------------------------------------------
  196.  
  197. FW_CFontIterator::FW_CFontIterator() :
  198.     fFontList(NULL),
  199.     fDeleteTheList(TRUE),
  200.     fIterator(NULL)    
  201. {
  202.     fFontList = FW_NEW(FW_CFontList, ());
  203.     fIterator = FW_NEW(FW_TOrderedCollectionIterator<FW_CString>, (&fFontList->fCollection));
  204.  
  205.     FW_END_CONSTRUCTOR
  206. }
  207.  
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // FW_CFontIterator::FW_CFontIterator
  211. //----------------------------------------------------------------------------------------
  212.  
  213. FW_CFontIterator::FW_CFontIterator(FW_CFontList* fontList) :
  214.     fFontList(fontList),
  215.     fDeleteTheList(FALSE),
  216.     fIterator(NULL)    
  217. {
  218.     FW_ASSERT(fontList != NULL);
  219.     
  220.     fIterator = FW_NEW(FW_TOrderedCollectionIterator<FW_CString>, (&fFontList->fCollection));
  221.  
  222.     FW_END_CONSTRUCTOR
  223. }
  224.  
  225. //----------------------------------------------------------------------------------------
  226. // FW_CFontIterator::~FW_CFontIterator
  227. //----------------------------------------------------------------------------------------
  228.  
  229. FW_CFontIterator::~FW_CFontIterator()
  230. {
  231.     FW_START_DESTRUCTOR
  232.     
  233.     if (fDeleteTheList)
  234.         delete fFontList;
  235.  
  236.     delete fIterator;
  237. }
  238.  
  239. //----------------------------------------------------------------------------------------
  240. // FW_CFontIterator::First
  241. //----------------------------------------------------------------------------------------
  242.  
  243. FW_CString FW_CFontIterator::First()
  244. {
  245.     FW_CString *name = fIterator->First();
  246.     return name ? *name : FW_CString("");
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. // FW_CFontIterator::Next
  251. //----------------------------------------------------------------------------------------
  252.                             
  253. FW_CString FW_CFontIterator::Next()
  254. {
  255.     FW_CString *name = fIterator->Next();
  256.     return name ? *name : FW_CString("");
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. // FW_CFontIterator::Last
  261. //----------------------------------------------------------------------------------------
  262.                             
  263. FW_CString FW_CFontIterator::Last()
  264. {
  265.     FW_CString *name = fIterator->Last();
  266.     return name ? *name : FW_CString("");
  267. }
  268.  
  269. //----------------------------------------------------------------------------------------
  270. // FW_CFontIterator::Previous
  271. //----------------------------------------------------------------------------------------
  272.                             
  273. FW_CString FW_CFontIterator::Previous()
  274. {
  275.     FW_CString *name = fIterator->Previous();
  276.     return name ? *name : FW_CString("");
  277. }
  278.  
  279. //----------------------------------------------------------------------------------------
  280. // FW_CFontIterator::IsNotComplete
  281. //----------------------------------------------------------------------------------------
  282.  
  283. FW_Boolean FW_CFontIterator::IsNotComplete()
  284. {
  285.     return fIterator->IsNotComplete();
  286. }
  287.  
  288.